export default (() => {
  let timer = null;
  let timestamp = null;
  let defaultConfig = { isDev: false, timeout: 20 /* 单位分钟 */ };
  let config = defaultConfig;
  return {
    config(option) {
      config = Object.assign({}, defaultConfig, config, option);
    },
    onLogout() {},
    start() {
      let { isDev, timeout } = config;
      if (isDev === true) {
        return;
      }
      if (typeof timeout !== 'number' || timeout <= 0) {
        timeout = defaultConfig.timeout;
      }
      this.restart();
      clearInterval(timer);
      timer = setInterval(() => {
        if ((Date.now() - timestamp) / 1000 / 60 > timeout) {
          clearInterval(timer);
          this.onLogout();
        }
      }, 60 * 1000);
      window.addEventListener('click', this.restart);
      window.addEventListener('keyup', this.restart);
      window.addEventListener('mousemove', this.restart);
    },
    restart() {
      timestamp = Date.now();
    },
  };
})();
